<template>
  <div class="app-container">
    <div
      v-show="
        !modifyInfo.visible &&
        !detailInfo.visible &&
        !assetAnalysisPlansDetailInfo.visible
      "
    >
      <!-- 条件栏 -->
      <div class="filter-wrapper">
        <NewPageFilter
          v-model:query="filterInfo.query"
          :filter-list="filterInfo.list"
          :filter-btn="filterInfo.btn"
        >
        </NewPageFilter>
      </div>
      <div class="filter-wrapper">
        <NewPageFilter :filter-btn="filterInfo.btnList">
          <template v-slot:filter-del>
            <el-button :disabled="!canDelete" @click="del()">删除</el-button>
          </template>
        </NewPageFilter>
      </div>
      <!-- 表格 -->
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-planNo="scope">
          <span
            v-if="scope.row.planNo"
            class="slip-edit"
            @click="showAnalysisPlansDetail(scope.row)"
            >{{ scope.row.planNo }}</span
          >
          <span v-else>-</span>
        </template>
      </TableContainer>
    </div>
    <Modify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :orderId="modifyInfo.orderId"
      :id="modifyInfo.id"
      :title="modifyInfo.title"
      @returnModify="returnModify"
    />
    <!-- 明细信息 -->
    <Detail
      v-if="detailInfo.visible"
      v-model:visible="detailInfo.visible"
      :id="detailInfo.id"
      :orderId="detailInfo.orderId"
      @returnBack="getList"
    />
    <!-- 方案明细信息 -->
    <AssetAnalysisPlansDetail
      v-if="assetAnalysisPlansDetailInfo.visible"
      v-model:visible="assetAnalysisPlansDetailInfo.visible"
      :id="assetAnalysisPlansDetailInfo.id"
    />
  </div>
</template>

<script setup name="assetCostConfig">
import { getAnalysisCostConfigPage, delAnalysisCostConfig } from '@/api/asset'
import { getDeptAll } from '@/api/core'
const Modify = defineAsyncComponent(() => import('./modify'))
const Detail = defineAsyncComponent(() => import('./details'))
const AssetAnalysisPlansDetail = defineAsyncComponent(() =>
  import('@/views/asset/analy/assetAnalysisPlans/detail')
)

const { proxy } = getCurrentInstance()

// 过滤相关配置
const filterInfo = ref({
  query: {
    sort: 'create_time desc',
  },
  copyQuery: {},
  list: [
    {
      componentType: 'input',
      label: '分析方案名称',
      value: 'planNameLike',
      event: search,
    },
    {
      componentType: 'input',
      label: '分析方案编码',
      value: 'planNoLike',
      event: search,
    },
    {
      label: '分析部门',
      value: 'deptIdMult',
      query: {
        status: 1,
        deptAuth: 'id',
      },
      api: getDeptAll,
      componentType: 'slip-select',
      multiple: true,
      event: search,
    },
    {
      value: 'analysisTypeValue',
      asValue: 'analysisTypeMult',
      componentType: 'select-model',
      code: 'analysis_type',
      label: '分析类型',
      event: search,
    },
  ],
  btn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btnList: [
    {
      type: 'button',
      label: '新增',
      event: create,
      show: proxy.$permission('assetCostConfig:add'),
    },
    {
      type: 'slot',
      value: 'del',
      show: proxy.$permission('assetCostConfig:del'),
    },
  ],
})

// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  api: getAnalysisCostConfigPage,
  data: [],
  selections: [],
  fieldList: [
    {
      label: '分析方案名称',
      value: 'planName',
    },
    {
      label: '分析方案编码',
      value: 'planNo',
      type: 'slot',
    },
    { label: '分析部门', value: 'deptName' },
    { label: '分析类型', value: 'analysisTypeValue' },
    { label: '创建人', value: 'createUserName' },
    { label: '创建时间', value: 'createTime' },
    { label: '修改人', value: 'updateUserName' },
    { label: '修改时间', value: 'updateTime' },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '100',
    btList: [
      {
        label: '修改',
        event: update,
        show: proxy.$permission('assetCostConfig:update'),
      },
      {
        label: '删除',
        event: del,
        popconfirmText: '删除后将改变数据,是否继续？',
        show: proxy.$permission('assetCostConfig:del'),
      },
      {
        label: '详情',
        event: toDetails,
        show: proxy.$permission('assetCostConfig:details'),
      },
    ],
  },
})
const modifyInfo = ref({
  visible: false,
  title: '',
  orderId: '',
  id: '',
})
const detailInfo = ref({
  visible: false,
  id: '',
  orderId: '',
})
const assetAnalysisPlansDetailInfo = ref({
  visible: false,
  id: '',
})

const canDelete = computed(() => {
  return tableInfo.value.selections.length
})
const toPage = computed(() => {
  return proxy.$store.state.settings.toPage
})
let stopWatch = function () {}
onActivated(() => {
  stopWatch = watch(
    () => toPage.value,
    (val) => {
      if (val) {
        let menus = proxy.$store.state.permission.defaultMenus
        menus.forEach((item) => {
          if (item.children) {
            item.children.forEach((el) => {
              if (el.code === 'assetCostConfig' && el.url === val.url) {
                modifyInfo.value.visible = false
                detailInfo.value.visible = false
                if (val.urlType === 1) {
                  getList()
                } else if (val.urlType === 2) {
                  detailInfo.value.visible = true
                  let data = {
                    id: val.businessId,
                  }
                  detailInfo.value.tableRow = data
                  sessionStorage.setItem('storeRowId', val.businessId)
                }
                proxy.$store.dispatch('settings/toPage', '')
              }
            })
          }
        })
      }
    },
    {
      deep: true,
      immediate: true,
    }
  )
})
onDeactivated(() => {
  stopWatch()
})
onMounted(() => {
  filterInfo.value.copyQuery = proxy.$fn.deepClone(filterInfo.value.query)
})

function returnModify() {
  getList()
}
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function clearSearch() {
  filterInfo.value.query = proxy.$fn.deepClone(filterInfo.value.copyQuery)
  search()
}
// 搜索
function search() {
  // 重置分页
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
//创建
function create() {
  modifyInfo.value.title = '新增支出费用配置'
  modifyInfo.value.visible = true
  modifyInfo.value.orderId = null
  modifyInfo.value.id = null
}
//更新
function update(row) {
  modifyInfo.value.title = '修改支出费用配置'
  modifyInfo.value.orderId = row.analysisPlanId
  modifyInfo.value.id = row.id
  modifyInfo.value.visible = true
}
//跳转方案编号详情
function showAnalysisPlansDetail(row) {
  assetAnalysisPlansDetailInfo.value.id = row.analysisPlanId
  assetAnalysisPlansDetailInfo.value.visible = true
}
function toDetails(row) {
  detailInfo.value.id = row.id
  detailInfo.value.orderId = row.analysisPlanId
  console.log(detailInfo.value.orderId)
  detailInfo.value.visible = true
}
//删除
function del(row = undefined) {
  const ids = _getIds(row)
  if (row && row.id) {
    delAnalysisCostConfig(ids).then((resp) => {
      if (resp && resp.success) {
        getList()
      }
    })
  } else {
    proxy
      .$confirm('删除后将改变数据,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
      .then(() => {
        delAnalysisCostConfig(ids).then((resp) => {
          if (resp && resp.success) {
            getList()
          }
        })
      })
  }
}
function _getIds(row) {
  let ids = []
  if (row && row.id) {
    ids = [row.id]
  } else {
    if (tableInfo.value.selections.length > 0) {
      ids = tableInfo.value.selections.map((row) => row.id)
    }
  }
  return ids
}
</script>

<style scoped></style>
